﻿using System.Collections.ObjectModel;
using System.Data;
using CAION2.Classes;
using CAION2.Commands;
using CAION2.Models;
using FirstFloor.ModernUI.Windows.Controls;

namespace CAION2.ViewModels
{
    class IdianDialogViewModel : ViewModelBase
    {
        #region Vars

        private Idian idian;
        private Idian selectedGroupIdian;
        private Idian selectedIdian;
        private ObservableCollection<Idian> groupIdians;
        private ObservableCollection<Idian> idians;
        private byte maxLevel;
        private string idianText;

        #endregion

        #region Properties

        public ModernDialog Dialog { private get; set; }

        public Idian Idian
        {
            get { return idian; }
            set
            {
                if (Equals(idian, value)) return;
                idian = value;

                OnPropertyChanged("Idian");

                UpdateIdianText();
            }
        }

        public Idian SelectedGroupIdian
        {
            get { return selectedGroupIdian; }
            set
            {
                if (Equals(selectedGroupIdian, value)) return;
                selectedGroupIdian = value;

                OnPropertyChanged("SelectedGroupIdian");

                UpdateIdians();
            }
        }

        public Idian SelectedIdian
        {
            get { return selectedIdian; }
            set
            {
                if (Equals(selectedIdian, value)) return;
                selectedIdian = value;

                OnPropertyChanged("SelectedIdian");

                if (SelectedIdian != null) Idian = SelectedIdian;
            }
        }

        public ObservableCollection<Idian> GroupIdians
        {
            get { return groupIdians; }
            set
            {
                if (Equals(groupIdians, value)) return;
                groupIdians = value;

                OnPropertyChanged("GroupIdians");
            }
        }

        public ObservableCollection<Idian> Idians
        {
            get { return idians; }
            set
            {
                if (Equals(idians, value)) return;
                idians = value;

                OnPropertyChanged("Idians");
            }
        }

        public byte MaxLevel
        {
            get { return maxLevel; }
            set
            {
                if (Equals(maxLevel, value)) return;
                maxLevel = value;

                OnPropertyChanged("MaxLevel");
            }
        }

        public string IdianText
        {
            get { return idianText; }
            set
            {
                if (Equals(idianText, value)) return;
                idianText = value;

                OnPropertyChanged("IdianText");
            }
        }

        #endregion

        #region Commands

        public RelayCommand<string> SelectQualityCommand { get; private set; }
        public RelayCommand DeleteIdianCommand { get; private set; }
        

        #endregion

        public IdianDialogViewModel()
        {
            SelectQualityCommand = new RelayCommand<string>(UpdateGroupIdians) { IsEnabled = true };
            DeleteIdianCommand = new RelayCommand(DeleteIdian) { IsEnabled = true };
        }

        private void UpdateGroupIdians(string quality)
        {
            DataTable idiansTable 
                = DataModel.GetTable("Polish",
                                     "name, translate" + DataModel.EngFix + ", icon_name, level, quality",
                                     "level <= " + MaxLevel + " AND quality = '" + quality + "' " +
                                     "GROUP BY name, translate" + DataModel.EngFix + ", icon_name, level, quality ");

            GroupIdians = new ObservableCollection<Idian>();

            foreach (DataRow row in idiansTable.Rows)
            {
                GroupIdians.Add(new Idian(row, true));
            }
        }

        private void UpdateIdians()
        {
            Idians = new ObservableCollection<Idian>();
            if (SelectedGroupIdian == null) return;

            DataTable idiansTable
                = DataModel.GetTable("Polish",
                         "id, name, translate" + DataModel.EngFix + ", icon_name, quality, attr_group_id, prob, random_attr1, random_attr2, random_attr3, random_attr4, random_attr5, random_attr6, random_attr7, random_attr8",
                         "name = '" + SelectedGroupIdian.Name + "' ORDER BY prob");

            foreach (DataRow row in idiansTable.Rows)
            {
                Idians.Add(new Idian(row));
            }
        }

        private void UpdateIdianText()
        {
            if (Idian == null || string.IsNullOrEmpty(Idian.Name))
            {
                IdianText = "";
                return;
            }

            IdianRandomTextConverter converter = new IdianRandomTextConverter();

            IdianText = string.Format(
                "{0}\n{1} - {2}",
                Idian.Translate,
                Idian.Chance,
                converter.Convert(Idian.Random1, null, null, null));

            string iBonus2 = converter.Convert(Idian.Random2, null, null, null).ToString();
            if (!string.IsNullOrWhiteSpace(iBonus2)) IdianText += ",  + " + iBonus2;

            iBonus2 = converter.Convert(Idian.Random3, null, null, null).ToString();
            if (!string.IsNullOrWhiteSpace(iBonus2)) IdianText += ", " + iBonus2;
        }

        private void DeleteIdian()
        {
            Idian = new Idian();
            Dialog.Close();
        }
    }
}
